package org.csu.webjpetstoreback.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.csu.webjpetstoreback.config.CommonResponse;
import org.csu.webjpetstoreback.mapper.SignonMapper;
import org.csu.webjpetstoreback.model.Account;
import org.csu.webjpetstoreback.model.Signon;
import org.csu.webjpetstoreback.service.SignonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service

public class SignonServiceImpl extends ServiceImpl<SignonMapper,Signon> implements SignonService {

    @Autowired
    private SignonMapper signonMapper;

    @Override
    public CommonResponse<Signon> login(String username,String password) {
        QueryWrapper<Signon> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        queryWrapper.eq("password",password);
        Signon signon = signonMapper.selectOne(queryWrapper);
        if (signon != null){
            signon.setPassword(null);
            return CommonResponse.BackForSuccess(signon);
        }else {
            return CommonResponse.BackForError("用户名或者密码错误");
        }
    }

    @Override
    public CommonResponse<Signon> PostAccount(Signon signon) {
        int result = signonMapper.insert(signon);
        if (result ==1){
            return CommonResponse.BackForSuccess(signon);
        }else{
            return CommonResponse.BackForError("新增失败");
        }
    }

    @Override
    public CommonResponse<Signon> PutAccount(Signon signon) {
        UpdateWrapper<Signon> UpdateWrapper = new UpdateWrapper<>();
        int result = signonMapper.update(signon,UpdateWrapper);
        if (result ==4){
            System.out.println("success in update");
            signon.setPassword(null);
            return CommonResponse.BackForSuccess(signon);
        }else{
            return CommonResponse.BackForError("密码修改失败");
        }
    }
}
